<template>
	<view class="container">
		<view class="info">
			<view class="top">
				<view class="title">
					资格认证信息：
				</view>
			</view>
			<view class="form_title">
				<view class="form_item">姓&emsp;&emsp;名：</view>
				<u-input v-model="name" placeholder="请输入姓名" border="bottom">
				</u-input>
			</view>
			<view class="form_title">
				<view class="form_item">身份证号：</view>
				<u-input v-model="id" placeholder="请输入身份证号" border="bottom">
				</u-input>
			</view>
			<view class="form_title">
				<view class="form_item2">上传贫困证明：</view>
			</view>
			<view class="picBox">
				<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="5" :previewFullImage="true"></u-upload>
			</view>

		</view>

		<view class="btn">
			<u-button text="提交信息" color="#d81e06" @click="admitForm"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		name: "Donate",
		data() {
			return {
				name: '',
				id: '',
				userId: '',
				fileList: [],
			};
		},
		onShow() {
			this.userId = uni.getStorageSync('userId')
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this.fileList.splice(event.index, 1)
			},
			// 新增图片
			afterRead(event) {
				var that = this
				const file = event.file[0]
				uni.uploadFile({
					url: 'http://192.168.31.240:8080/oss/fileoss',
					filePath: file.url,
					name: 'file',
					success: (uploadFileRes) => {
						const res = JSON.parse(uploadFileRes.data)
						that.fileList.push({
							url: res.data.url
						})
					},

				});
			},
			//确认并提交信息
			async admitForm() {
				var that = this;
				// 验证表单各项信息是否为空
				if (!this.name) {
					uni.showToast({
						title: '姓名不能为空！',
						icon: 'none'
					})
				} else if (!this.id) {
					uni.showToast({
						title: '身份证号不能为空！',
						icon: 'none'
					})
				} else if (!this.fileList.length) {
					uni.showToast({
						title: '请至少上传一张图片！',
						icon: 'none'
					})
				}
				// 提交表单
				const {data:res} = await this.$myRequest({
					url: '/userAuthBefore/addUserAuth',
					method: 'post',
					data: {
						userId: that.userId,
						picture: that.fileList[0].url
					},
				})
				console.log(res.code);
				if (res.code==20000) {
					uni.showToast({
						title: '提交信息成功,请等待审核。',
						icon: 'none'
					});
					setTimeout(function() {
						uni.navigateBack()
					}, 1500);
				}

			}


		},
	}
</script>

<style lang="scss" scoped>
	.container {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 20rpx;
		margin: 20rpx;

		.info {
			padding: 0 20rpx;

			.top {
				display: flex;
				justify-content: space-between;
				margin-bottom: 50rpx;

				.title {
					font-size: larger;
					font-weight: bold;
				}
			}

			.form_title {
				display: flex;
				margin-bottom: 40rpx;

				.form_item {
					width: 160rpx;
					display: flex;
					align-items: center;
					justify-content: flex-end;

				}
			}

			.form_item2 {
				margin: 10rpx 0;
			}

		}

		.btn {
			width: 200rpx;
			margin: 20rpx auto 20rpx;
		}

	}
</style>
